package babo.cd.physicalexperiment.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;

import java.io.IOException;

import babo.cd.physicalexperiment.activity.LoginActivity;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class OKCallback implements Callback {
    Handler handler=new Handler();
    Context context;
    public OKCallback(Context context){
        this.context=context;
    }
    @Override
    public void onFailure(Call call, IOException e) {
    Out.out("请求错误："+e.getMessage());
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        String body=response.body().string();
        int code=response.code();
        Out.out("请求返回code=="+code);
        Out.out("请求返回body="+body);
//        Out.out("请求返回header="+response.headers());
        if (code==200){
            handler.post(new Runnable() {
                @Override
                public void run() {
                    Sucess(body);

                }
            });

        }else{

            handler.post(new Runnable() {
                @Override
                public void run() {
                    if (code==401){
                        Intent intent=new Intent(context, LoginActivity.class);
                        context.startActivity(intent);
                        try {
                            ((Activity)context).finish();
                        }catch (Exception ex){}
                    }else{
                        Eorror(code);
                    }


                }
            });

        }


    }
    public void Sucess(String str){}
    public void Eorror(int code){}
}
